//引入express
var express = require('express');
//引入路由
var apps = express.Router();
var UUID = require('uuid');
let db=require('../utils/db');
apps.get('/ceshiget111', function (req, res) {
  res.json({ val: 'g3333333' });
})
apps.post('/ceshipost', function (req, res) {
  // res.json({ val: 'post' });
})

//店铺管理
//获取所有店铺
apps.get('/getAllStore',function(req,res){
  let sql='select * from store limit '+(req.query.page-1)*req.query.pageSize+','+req.query.pageSize+'';
  let sql1='select count(id) count from store';
  db.query(sql,function(err,data){
    if(data){
      db.query(sql1,function(err,data1){
        res.json({
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})
//更改店铺关注
apps.post('/storeFollow',function(req,res){
  // res.json({ val: 'get111' });
  let sql='';
  if(req.body.attention=='关注'){
    sql='update store set attention="取关" where id='+req.body.id+'';
  }else{
    sql='update store set attention="关注" where id='+req.body.id+'';
  }
  db.query(sql,function(err,data){
    res.json(data)
  })
})
//获取已关注店铺
apps.get('/getFollowStore',function(req,res){
  let sql='select * from store where attention="'+req.query.attention+'" limit '+(req.query.page-1)*req.query.pageSize+','+req.query.pageSize+'';
  let sql1='select count(id) count from store where attention="'+req.query.attention+'"';
  db.query(sql,function(err,data){
    console.log(sql);
    if(data){
      db.query(sql1,function(err,data1){
        res.json({
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})
//获取未关注店铺
apps.get('/getNoFollowStore',function(req,res){
  let sql='select * from store where attention="'+req.query.attention+'" limit '+(req.query.page-1)*req.query.pageSize+','+req.query.pageSize+'';
  let sql1='select count(id) count from store where attention="'+req.query.attention+'"';
  db.query(sql,function(err,data){
    console.log(sql);
    if(data){
      db.query(sql1,function(err,data1){
        res.json({
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})
//查询店铺
apps.get('/findStore',function(req,res){
  let sql='select * from store where name like "%'+req.query.name+'%" limit '+(req.query.page-1)*req.query.pageSize+','+req.query.pageSize+'';
  let sql1='select count(id) count from store where name like "%'+req.query.name+'%"';
  db.query(sql,function(err,data){
    if(data){
      db.query(sql1,function(err,data1){
        res.json({
          data:{data,count:data1[0].count}
        })
      })
    }else{
      res.json(err)
    }
  })
})

//店铺概览
apps.get('/storeMsg',function(req,res){
  let sql='SELECT SUM(b.actual_payment) AS sumprice,SUM(b.ordernum) AS sumnum FROM store a LEFT JOIN orderr b ON a.id=b.store_id WHERE a.id="'+req.query.id+'"';
  db.query(sql,function(err,data){
    res.json(data)
  })
})
//店铺详情
apps.get('/storeDetails',function(req,res){
  // res.json({ val: 'get111' });
  let sql='select * from store where id="'+req.query.id+'"';
  db.query(sql,function(err,data){
    res.json(data)
  })
})

//C端登录
// apps.get('/Clogin',function(req,res){
//   let sql='SELECT * FROM USER WHERE user_name='+req.query.user_name+' AND PASSWORD='+req.query.pass+'';
//   db.query(sql,function(err,data){
//     console.log(data);
//     if(data){
//       res.json({val:'登录成功'})
//     }else{
//       res.json({val:'登陆失败'})
//     }
//   })
// })

//C端商品详情
apps.get('/getGoodDetail',function(req,res){
  let sql='SELECT * FROM goods WHERE id="'+req.query.id+'"';
  // let sql='SELECT * FROM goods a LEFT JOIN goods_classify b ON a.goods_classify_id=b.goods_classify_child_id WHERE id="'+req.query.id+'"';
  // let sql='SELECT * FROM goods a LEFT JOIN goods_classify_child b ON a.goods_classify_id=b.goods_classify_id WHERE a.id="'+req.query.id+'"';
  db.query(sql,function(err,data){
    console.log(sql);
    console.log(data);
    res.json({
      data
    })
  })
})

//C端购物车数量
apps.get('/getAllCart',function(req,res){
  let sql='SELECT COUNT(*) as unm FROM cart'
  db.query(sql,function(err,data){
    res.json({
      data
    })
  })
})
// 获取所有商店对应的订单
apps.get('/getAllorderPrice', function(req, res) {
  let sql = 'SELECT store_id,SUM(ordernum)AS num, SUM(actual_payment)AS payment FROM orderr GROUP BY store_id'
  db.query(sql, function(err, data) {
    if (data) {
      res.json({
        code: 200,
        msg: '查询成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '查询失败'
      })
    }
  })
})

//b端订单收益
apps.get('/getAllPrice',function(req,res){
  let sql='SELECT SUM(actual_payment)AS num FROM orderr'
  db.query(sql,function(err,data){
    res.json({
      data
    })
  })
})


//b获取会员
apps.get('/getVipUser',function(req,res){
  let sql='SELECT * FROM `user`'
  db.query(sql,function(err,data){
    res.json({
      data
    })
  })
})




//PC以失效订单
apps.get('/getEndOrder',function(req,res){
  let sql='SELECT a.id,c.name,b.`user_name`,b.`tel`,a.`order_address`,e.`image_url`,e.`information`,e.`sell_price`,e.num,a.`state`,a.type,e.id goods_id FROM orderr a LEFT JOIN `user` b ON a.`user_id`=b.id LEFT JOIN store c ON a.`store_id`=c.id LEFT JOIN order_goods_center d ON d.`order_id`=a.id LEFT JOIN goods e ON e.id=d.`goods_id` WHERE a.id="'+req.query.id+'" AND a.state=5 AND a.`deleted`=0'
  db.query(sql,function(err,data){
    console.log(data);
    let allRes={};
    let resGoodsArr=[];
    for(let i in data){
      switch(data[i].state){
        case 1:data[i].state='待付款';break;
        case 2:data[i].state='待发货';break;
        case 3:data[i].state='待收货';break;
        case 4:data[i].state='已完成';break;
        case 5:data[i].state='已失效';break;
      }
      switch(data[i].type){
        case 0:data[i].type='未结算';break;
        case 1:data[i].type='已结算';break;
      }
      let json={
        imageUrl:data[i].image_url,
        information:data[i].information,
        sellPrice:data[i].sell_price,
        num:data[i].num,
        state:data[i].state,
        type:data[i].type,
        key:data[i].goods_id
      }
      resGoodsArr.push(json);
    }
    allRes={
      id:data[0].id,
      name:data[0].name,
      userName:data[0].user_name,
      tel:data[0].tel,
      orderAddress:data[0].order_address,
      goodsArr:resGoodsArr
    }

    res.json({
      cood:200,
      data:allRes
    })
  })
})
module.exports=apps;